package com.atguigu.gmall.order.task;

import com.atguigu.gmall.order.delay.OrderDelayTask;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBlockingQueue;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Set;

//@Component
@Slf4j
public class OrderDelayTaskScheduled {

    @Autowired
    private RedisTemplate<String , String> redisTemplate ;

    @Autowired
    private RedissonClient redissonClient ;

    @Scheduled(cron = "0/1 * * * * ?")
    public void orderDelayTask() {
        RBlockingQueue<Object> rBlockingQueue = redissonClient.getBlockingQueue("order_close_delay_queue");
        Object poll = rBlockingQueue.poll();
        if(poll != null) {
            log.info("延迟任务执行了...orderId:" + poll);
        }
    }

//    @Scheduled(cron = "0/1 * * * * ?")
//    public void orderDelayTask() {
//        log.info("OrderDelayTaskScheduled...orderDelayTask...执行了...");
//        Long time = System.currentTimeMillis() ;
//        Set<String> score = redisTemplate.opsForZSet().rangeByScore("order:delay:queue" , Double.MIN_VALUE , time );
//        redisTemplate.opsForZSet().removeRangeByScore("order:delay:queue" , Double.MIN_VALUE , time) ;
//        score.forEach(orderId -> {
//            log.info("延迟任务执行了...orderId:" + orderId);
//        });
//    }

//    @Autowired
//    private DelayQueue<OrderDelayTask> delayQueue ;
//
//    @Scheduled(cron = "0/1 * * * * ?")
//    public void orderTask() {
//        OrderDelayTask orderDelayTask = delayQueue.poll();
//        if(orderDelayTask != null) {
//            Long orderId = orderDelayTask.getOrderId();
//            System.out.println("获取到了延迟任务执行业务操作， orderId：" + orderId);
//        }
//    }

}
